package com.springboot.web.controller;

import com.springboot.web.bean.User;
import com.springboot.web.common.ServiceException;
import com.springboot.web.common.ServiceExceptionEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController//Controller类上加了@RestController注解，等价于在类中的每个方法上都加了@ResponseBody
@Slf4j
@RequestMapping("/json")
public class JsonController{    
	@RequestMapping("/test1")
    @ResponseBody //将handler的返回值，转换成json(jackson),并将json响应给客户端。
    public User test1(){
        return new User(1,"aaa",new Date(),false);
    }
    // @ResponseBody还可以用在handler的返回值上
    @RequestMapping("/test2")
    public @ResponseBody List<User> test2(){
        return List.of(
                new User(1, "aaa", new Date(), true),
                new User(2, "bbb", new Date(), false),
                new User(3, "ccc", new Date(), true)
        );
    }
    // 如果返回值已经是字符串，则不需要转json，直接将字符串响应给客户端 
    @RequestMapping(value="/test3")
    @ResponseBody 
    public String test3(){
        return "你好";
    }

    @PostMapping("/users")
    public String addUser(@RequestBody User user){//@RequestBody将请求体中的json数据转换为java对象
        log.info("Post user :"+user);
        return "success";
    }

    @DeleteMapping("/users/{id}")
    public String deleteUser(@PathVariable Integer id){
        if(id < 10)
            throw new ServiceException(ServiceExceptionEnum.USER_NOT_FOUND);
        log.info("Delete user id:"+id);
        return "success";
    }

    @PutMapping("/users")
    public String updateUser(@RequestBody User user){
        log.info("Put user :"+user);
        return "success";
    }
}